package com.carlinfo.stu_ques.question.service.impl;


import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.carlinfo.stu_ques.common.util.DateUtile;
import com.carlinfo.stu_ques.question.dao.IAnswerDao;
import com.carlinfo.stu_ques.question.dao.IQuestionDao;
import com.carlinfo.stu_ques.question.pojo.Answer;
import com.carlinfo.stu_ques.question.pojo.Question;
import com.carlinfo.stu_ques.question.service.IQuestionService;
import com.carlinfo.stu_ques.users.pojo.Students;
import com.carlinfo.stu_ques.users.service.Istudentsservice;

public class QuestionServiceImpl implements IQuestionService
{
	private IAnswerDao answer  ;
	private IQuestionDao question ;

	private Istudentsservice studntsservice  ;
	
	
	DateUtile dateutile = new DateUtile();
	@Override
	public int saveOneAnswerService(Answer answer)
	{
		int id = this.answer.savaOneDao(answer);
		Map<String, Object> condMap = new HashMap();
		condMap.clear();
		condMap.put("id", answer.getQuestionId());
		Question question = this.selectOneQuestionService(condMap);
		/* 要修改一些属性 */
		/* 查询所有的答案,把此问题的答案列出来 */
		int count = 0;
		List<Answer> answerList = this.selectListAnswerService();
		for (Iterator iterator = answerList.iterator(); iterator.hasNext();)
		{
			Answer answTemp = (Answer) iterator.next();
			/* 如果循环的答案的问题id和目标的答案问题id一致 */
			if (answer.getQuestionId() == answTemp.getQuestionId())
			{
				/* 你的爸和我的爸一样,咱两是兄弟 */
				count++;
			}
		}
		question.setPeopleNum(count);
		this.updateOneQuestionService(question);
		/* 更新学生 */
		condMap.clear();
		condMap.put("id", answer.getSdId());
		Students students = this.studntsservice.selectStudentsOneDao(condMap);
		/* 总次数+1 */
		int totalCount = students.getTodayCount();
		students.setTotalCount(totalCount + 1);
		/*
		 * 如何计算今天的总次数
		 */
		Date updateTime = (Date) students.getUpdateTime();
		boolean isyesFlag = this.dateutile.isyesDay(new Date(), updateTime);
		if(isyesFlag) 
		{
			students.setTodayAccuracy(0);
			students.setTodayCount(0);
			students.setCountFalse(0);
			students.setTodayTrueCount(0);
			System.out.println("我清零了");
		}
		students.setTodayCount(students.getTodayCount() + 1);
		if("正确".equalsIgnoreCase(answer.getFolg()))
		{
			/* 正确 */
			students.setCountTrue(students.getCountTrue() + 1);
			students.setTodayTrueCount(students.getTodayTrueCount() + 1);
			students.setLevel(students.getLevel()+answer.getLevel());
		}else
		{
			/* 错误 */
			students.setCountFalse(students.getCountFalse() + 1);
			students.setTodayFalseCount(students.getTodayFalseCount() + 1);
			students.setLevel(students.getLevel()+answer.getLevel());
		}
		/* 正确率 */
		students.setAccuracy(students.getCountTrue() / students.getTotalCount());
		students.setTodayAccuracy(students.getTodayTrueCount() / students.getTodayCount());
		students.setUpdateTime(new Date());
		/* 赶紧赶紧更新学生信息 */
		this.studntsservice.updateStudentsOneDao(students);
		return id;
	}

	@Override
	public void updateOneAnswerService(Answer answer)
	{
		this.answer.updateOneDao(answer);

	}

	@Override
	public void deleteOneAnswerService(Answer answer)
	{
		this.answer.deleteOneDao(answer);

	}

	@Override
	public List<Answer> selectListAnswerService()
	{
//		return this.answer.selectListDao();
		List<Answer> answerList = this.answer.selectListDao();
		Map<String, Object> condMap1 = new HashMap<String, Object>();

		/* 循环为问题和学生赋值 */
		for (Answer ansTemp : answerList)
		{
			/* 查询问题:根据questionId查询问题 */
			condMap1.clear();
			condMap1.put("id", ansTemp.getQuestionId());
			Question question = this.selectOneQuestionService(condMap1);
			ansTemp.setQuestion(question);
			/* 查询学生:根据studentId查询学生 */
			condMap1.clear();
			condMap1.put("id", ansTemp.getSdId());
			/*
			 * 为啥在当前的Service中木有查询一个学生的方法 目的:是为了查询学生;(提需求:在答案列表中,显示学生的名字和等级)
			 * ~做法1:直接调用学生dao的相关方法;请问壮:写到了usersService中; 请问:学生的等级还能否赋值呢?
			 * ~做法2:直接调用usersService,让usersService调用学生的dao
			 */
			Students students = this.studntsservice.selectStudentsOneDao(condMap1);
			ansTemp.setStudent(students);
		}
		return answerList;
	}

	@Override
	public Answer selectOneAnswerService(Map<String, Object> condMap)
	{
		return this.answer.selectOneDao(condMap);	
	}

	@Override
	public String printTitleAnswerService()
	{
		String result = "id\t学生id\t答案\t对错\t\t\t创建时间\t\t\t结束时间";
		return result;
	}

	@Override
	public int saveOneQuestionService(Question Question)
	{
		int id = this.question.savaOneDao(Question);
		return id;
	}

	@Override
	public void updateOneQuestionService(Question Question)
	{
		this.question.updateOneDao(Question);

	}

	@Override
	public void deleteOneQuestionService(Question Question)
	{
		this.question.deleteOneDao(Question);

	}

	@Override
	public List<Question> selectListQuestionService()
	{
		return this.question.selectListDao();
	}

	@Override
	public Question selectOneQuestionService(Map<String, Object> condMap)
	{
		return this.question.selectOneDao(condMap);
	}

	@Override
	public String printTitleQuestionService()
	{
		String result = "id\t名字\t分数\t回答人数\t\t\t创建时间\t\t\t结束时间";
		return result;
	}

	public void setAnswer(IAnswerDao answer)
	{
		this.answer = answer;
	}

	public void setQuestion(IQuestionDao question)
	{
		this.question = question;
	}

	public void setStudntsservice(Istudentsservice studentservice)
	{
		this.studntsservice = studentservice;
	}

}
